﻿using EYWeb.Core;
using EYWeb.Domain.Entities;
using EYWeb.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EYWeb.WebLTE.Controllers
{
    public class LoginController : Controller
    {
        private readonly IUserService _userService;
        private readonly ILogService _logService;

        public LoginController(IUserService userService, ILogService logService)
        {
            _userService = userService;
            _logService = logService;
        }

        // GET: Login
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(VerifyCode.GetVerifyCode(), @"image/Gif");
        }

        [HttpPost]
        public ActionResult CheckLogin(string user, string pass, string code)
        {
            LogEntity logEntity = new LogEntity
            {
                ModuleName = "系统登录",
                Type = DbLogType.Login.ToString()
            };
            try
            {
                if (Session["Verifycode"].IsEmpty() || Md5.md5(code.ToLower(), 16) != Session["Verifycode"].ToString())
                {
                    throw new Exception("验证码错误,请重新输入");
                }
                UserEntity userEntity = _userService.CheckLogin(user, pass);
                if (userEntity != null)
                {
                    OperatorModel operatorModel = new OperatorModel
                    {
                        UserId = userEntity.Id,
                        UserCode = userEntity.Account,
                        UserName = userEntity.RealName,
                        CompanyId = userEntity.OrganizeId,
                        DepartmentId = userEntity.DepartmentId,
                        RoleId = userEntity.RoleId,
                        LoginIPAddress = Net.Ip
                    };
                    operatorModel.LoginIPAddressName = Net.GetLocation(operatorModel.LoginIPAddress);
                    operatorModel.LoginTime = DateTime.Now;
                    operatorModel.LoginToken = DESEncrypt.Encrypt(Guid.NewGuid().ToString());
                    operatorModel.IsSystem = userEntity.Account.ToLower() == "admin" ? true : false;
                    OperatorProvider.Provider.AddCurrent(operatorModel);

                    logEntity.Account = userEntity.Account;
                    logEntity.NickName = userEntity.RealName;
                    logEntity.Result = true;
                    logEntity.Description = "登录成功.";
                    _logService.WriteDbLog(logEntity);

                }
                return Content(new AjaxResult { state = ResultType.success.ToString(), message = "登录成功。" }.ToJson());
            }
            catch (Exception e)
            {
                logEntity.Account = user;
                logEntity.NickName = user;
                logEntity.Result = false;
                logEntity.Description = "登录失败," + e.Message;
                _logService.WriteDbLog(logEntity);
                return Content(new AjaxResult { state = ResultType.error.ToString(), message = e.Message }.ToJson());
            }
        }

        [HttpPost]
        public ActionResult Logout()
        {
            LogEntity logEntity = new LogEntity
            {
                ModuleName = "系统登录",
                Type = DbLogType.Exit.ToString(),
                Account = OperatorProvider.Provider.GetCurrent().UserCode,
                NickName = OperatorProvider.Provider.GetCurrent().UserName,
                Result = true,
                Description = "安全退出系统."
            };
            _logService.WriteDbLog(logEntity);

            Session.Abandon();
            Session.Clear();
            OperatorProvider.Provider.RemoveCurrent();
            return RedirectToAction("Index", "Login");
        }



    }
}